package fm.dongman.modellib.source;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;

import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.ArticleListContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * Created by KUIGE on 2017/10/30.
 */

public class ArticleListRepository implements ArticleListContract.IArticleListDataSource {
    private static final int mSize = 10; // 每页的数据量
    private static ArticleListRepository SINGLETON;

    public static final String TYPE_COSPLAY = "cosplay";
    public static final String TYPE_CARD = "dongman";
    public static final String TYPE_INVENTORY = "topic";
    public static final String TYPE_COMIC = "shouhui";
    public static final String TYPE_INFO = "zixun";

    private RemoteOperationHelper mRemoteOperationHelper;

    private int mPage;
    private String mType;
    private String mId;
    private boolean isRemote = true;

    private ArticleListRepository() {
        mRemoteOperationHelper = RemoteOperationHelper.getOperationHelper();
    }

    public static ArticleListRepository getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new ArticleListRepository();
        }
        return SINGLETON;
    }

    @Override
    public void setArticleListInfo(int page, @NotNull String type, @Nullable String id) {
        mPage = page;
        mType = type;
        mId = id;
    }

    @Override
    public void searchArticles(@NotNull String key, @NotNull LoadSourceCallback<? super List<? extends IModel.IArticleModel>> callback) {
        if (isRemote) {
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SEARCH_Q + "q=" + key + "&page=" + mPage + "&size=" + mSize + "&type=article");
            if (jsonObject == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemoteOperationHelper.downArticleList(jsonObject));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super List<? extends IModel.IArticleModel>> callback) {
        if (isRemote) {
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.FIND_ARTICLE_REMOTE + "page=" + mPage + "&size=" + mSize + "&type=" + mType);
            if (jsonObject == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemoteOperationHelper.downFArtcileList(jsonObject));
        }else  callback.onDataLoadFiled();
    }

    @Override
    public void saveData(List<? extends IModel.IArticleModel> dataModel) {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
